﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CubicFWNet.Application.SystemManager;
using CubicFWNet.Common;
using CubicFWNet.Data.Models;
using CubicFWNet.Mvc.Models;

namespace CubicFWNet.Mvc.Controllers
{
    public class StorageController : ControllerBase
    {
        private StorageApp _storageApp;

        public StorageController() {
            _storageApp = new StorageApp();
        }

        [Authenticate]
        [HttpGet]
        public ViewResult Index() {
            ViewBag.Lists = _storageApp.GetList();
            return View(_storageApp.GetList());
        }
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetFormJson(int id)
        {
            var data = _storageApp.GetForm(id);
            return Content(data.ToJson());
        }

        [HttpPost]
        [HandlerAjaxOnly]
        public string Delete(int id)
        {
            try
            {
                var del = _storageApp.GetForm(id);
                if (del == null)
                {
                    Result.Status = "error";
                    Result.Message = "未找到该数据";
                    Result.ToJson();
                }
                _storageApp.Delete(id);
            }
            catch (Exception e)
            {
                Result.Status = "error";
                Result.Message = e.Message;
            }
            return Result.ToJson();
        }
        [HttpPost]
        [HandlerAjaxOnly]
        public string Add(Storage entity) {
            try
            {
                entity.CreatorId = operatorModel.UserId;
                _storageApp.AddOrUpdate(entity);
            }
            catch (Exception ex)
            {
                this.Result.Status = "error";
                this.Result.Message = ex.Message;
            }
            return this.Result.ToJson();
        }
    }
}